<template>
  <el-dialog
      :title="form.id ? '编辑知识节点' : '新增知识节点'"
      :visible.sync="show"
      width="600px"
      @close="onClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="节点名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入节点名称"/>
      </el-form-item>
      <el-form-item label="节点类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择节点类型" style="width: 100%">
          <el-option label="概念" :value="1"/>
          <el-option label="定理" :value="2"/>
          <el-option label="公式" :value="3"/>
          <el-option label="规则" :value="4"/>
          <el-option label="设备" :value="5"/>
          <el-option label="技术" :value="6"/>
        </el-select>
      </el-form-item>
      <el-form-item label="专业领域" prop="domain">
        <el-select v-model="form.domain" placeholder="请选择专业领域" style="width: 100%">
          <el-option label="铁道工程" :value="1"/>
          <el-option label="信号控制" :value="2"/>
          <el-option label="机车车辆" :value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item label="难度等级" prop="level">
        <el-rate
            v-model="form.level"
            :max="5"
            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
        />
      </el-form-item>
      <el-form-item label="节点内容" prop="content">
        <el-input
            type="textarea"
            v-model="form.content"
            :rows="6"
            placeholder="请输入节点内容"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "KnowledgeNodeAdd",
  data() {
    return {
      show: false,
      form: {
        id: null,
        name: '',
        type: '',
        domain: '',
        level: 1,
        content: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入节点名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择节点类型', trigger: 'change' }
        ],
        domain: [
          { required: true, message: '请选择专业领域', trigger: 'change' }
        ],
        level: [
          { required: true, message: '请选择难度等级', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入节点内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    open(row) {
      this.show = true
      if (row) {
        this.form = { ...row }
      } else {
        this.form = {
          id: null,
          name: '',
          type: '',
          domain: '',
          level: 1,
          content: ''
        }
      }
    },
    onClose() {
      this.show = false
      this.$refs.form.resetFields()
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = { ...this.form }
          if (data.id) {
            http.knowledgeNode.update({
              ...data,
              createTime: undefined,
              updateTime: undefined
            }).then(() => {
              this.$message.success('编辑成功')
              this.onClose()
              this.$emit('get-page')
            })
          } else {
            http.knowledgeNode.create(data).then(() => {
              this.$message.success('新增成功')
              this.onClose()
              this.$emit('get-page')
            })
          }
        }
      })
    }
  }
}
</script>
